﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace 飞鸽传书
{
    public partial class Form1 : Form
    {
        TcpListener server;
        TcpClient tcpclient;
        IPAddress myIP;
        int myPort = 1499;





        //---------------------------
        private Thread thread;
        bool isstop = true;
        TcpClient socket;
        delegate void dFun(string text);
        dFun dfun1;
        delegate void cFun(ListViewItem lvi);
        cFun cfun1;


        public Form1()
        {
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            #region
            myIP = (IPAddress)Dns.GetHostAddresses(Dns.GetHostName()).GetValue(0);
           
            for (int i = 0; i < 51; i++)
            {
                try
                {
                    server = new TcpListener(myIP, myPort);
                    server.Start();
                    label1.Text = myIP + ":" + myPort;
                    break;
                }
                catch
                {
                    myPort++;
                }
                if (i == 50)
                {
                    MessageBox.Show("不能建立服务器");
                    return;
                }
           
            }
            server.Stop();
            #endregion

            isstop = false;
            dfun1 = new dFun(receiveMessage);
            thread = new Thread(new ThreadStart(ReadThread));
            thread.Start();

        }
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtMessage.Text == string.Empty)
            {
                MessageBox.Show("发送内容不能为空！");
                return;
            }

            ListView.SelectedListViewItemCollection I = listView1.SelectedItems;
            if (I.Count == 0)
            {
                MessageBox.Show("请选择好友");
                return;
            }

            tcpclient = new TcpClient();
            NetworkStream netstream = null;

            string ip;
            int port;
            for (int i = 0; i < I.Count; i++)
            {
                ip = I[i].SubItems[0].Text;
                port = Int32.Parse(I[i].SubItems[1].Text);
                try
                {
                    tcpclient.Connect(ip, port);
                    netstream = tcpclient.GetStream();
                    if (netstream.CanWrite)
                    {
                        string s1 = myIP.ToString() + ":" + myPort.ToString() + "说：  " + txtMessage.Text;
                        byte[] sendbyte = Encoding.UTF8.GetBytes(s1);
                        netstream.Write(sendbyte, 0, sendbyte.Length);
                    }
                }
                catch
                {

                    MessageBox.Show("发送失败");
                    break;
                }
                finally
                {
                    if (netstream != null)
                        netstream.Close();

                }
                tcpclient.Close();

            }
        }

        private void receiveMessage(string text)
        {
            txtMessage.Text = text;
        }
        private void SetFrindListView(ListViewItem lvi)
        {
            listView1.Items.Add(lvi);
        }

        private void ReadThread()
        {
            server.Start();
            while (!isstop)
            {
                try
                {
                    socket = server.AcceptTcpClient();
                    NetworkStream netstream = socket.GetStream();
                    StreamReader sr = new StreamReader(netstream);
                    string receiveMessage = sr.ReadLine();
                    txtMessage.Invoke(dfun1, new object[] {receiveMessage });
                }
                catch
                {

                }
            }
        }

        private void btnAddFriend_Click(object sender, EventArgs e)
        {
            string[] s = new string[] {textBox1.Text,textBox2.Text};
            ListViewItem lvi = new ListViewItem(s);
            listView1.Items.Add(lvi);
        }

    }
}
